using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Xml;

using MediaWikiEngine.Domain;

namespace MediaWikiEngine.Command
{
	/// <summary>
	/// Summary description for UploadFile
	/// </summary>
	public class UploadFile : BaseCommand
	{
		public UploadFile(CommandParams param, WikiConnection conn) : base(param, conn)
		{
		}

        public UploadFile(WikiConnection conn, string path, string mimetype, string destinationFilename, string description, string license) : base(conn)
        {
            CommandParams parameters = new CommandParams();
            parameters["DestinationFilename"] = destinationFilename;
            parameters["Path"] = path;
            parameters["MimeType"] = mimetype;
            parameters["Description"] = description;
            parameters["License"] = license;

            _params = parameters;
        }


		/// <summary>
		/// Creates a Wiki Get Page command using an Xml text writer.
		/// </summary>
		/// <param name="inXW"></param>
		/// <param name="inDescription"></param>
        /// <param name="inLicense">The license for the upload, if any.</param>
        /// <param name="inMimeType">The MIME type of the upload.</param>
		/// <param name="inFileName"></param>
		static public void ToXml(
			XmlTextWriter inXW,
            string inDestinationFilename,
			string inDescription,
			string inFileName,
            string inMimeType,
            string inLicense)
		{
			StartCommandToXml(inXW, "wiki:uploadfile");

			StartConditionsToXml(inXW);
			IfConditionTestToXml(inXW, "$loginsuccess", "equals", "True", "bot:exit");
			EndConditionsToXml(inXW);

			StartInputToXml(inXW);
            InputNameValueToXml(inXW, "DestinationFilename", inDestinationFilename); 
			InputNameValueToXml(inXW, "Description", inDescription);
			InputNameValueToXml(inXW, "MimeType", inMimeType);
			InputNameValueToXml(inXW, "Path", inFileName);
            InputNameValueToXml(inXW, "License", inLicense);
			EndInputToXml(inXW);

			EndCommandToXml(inXW);
		}

		public override CommandResult Execute()
		{
			NameObjectCollection formData = new NameObjectCollection();

			formData.Add("wpUploadFile", new FileDescription(_params["Path"].ToString(), _params["MimeType"].ToString()));
    
            if (true == _params.Contains("DestinationFilename"))
                formData.Add("wpDestFile", _params["DestinationFilename"]);
			
            formData.Add("wpUploadDescription", _params["Description"]);
			formData.Add("wpUploadAffirm", "1");
			formData.Add("wpIgnoreWarning","1");
			formData.Add("wpUpload", "Upload file");
            if (_params.Contains("License") && null != _params["License"] && ((string)_params["License"]).Length > 0)
                formData.Add("wpLicense", (string)_params["License"]);

			_conn.DoPostForm(_conn.Endpoint.UploadPage(), formData);

			return new UploadFileResult(new Hashtable());
		}

	}

    public class UploadFileResult : CommandResult
    {
        public UploadFileResult(Hashtable results)
            : base(results)
        {
        }

    }
}
